<?php
	class signup extends spController
	{
		function __construct()
		{
				// 必须加入启动父类构造函数的操作
				parent::__construct();
				 
				// 开始全局操作
				$this->arr_cat = Product_Cat();
				$this->a = $_GET['a'];
				$this->c = $_GET['c'];
		}

		function index()
		{
			if($_GET['from'] == 'itaobao') $this->jump('/index.php?from=itaobao');
			$m_signup = spClass('m_signup');
			
			$visitor_id = $_SESSION['visitor_id'];
			$visitor_nick = $_SESSION['visitor_nick'];
			$this->url = 'http://tb.banjiahui.com/index.php?c=click&a=w_go&wid=11';
			$this->islogined = $visitor_id ? true : false;
			if($this->islogined) {
				$visitor_id = intval($visitor_id);
				if($visitor_id < 1) return false;
				$this->signup_result = $m_signup->findSql("SELECT * FROM `signup` s LEFT JOIN `sign_invite` si ON s.si_id=si.si_id WHERE s.visitor_id='$visitor_id' AND s.si_id=0");
				$this->signuped = $this->signup_result ? true :false;
				$this->url = 'http://tb.banjiahui.com/index.php?c=click&a=w_go&wid=11&invite_id=' . $visitor_id;
			}
			
			$this->latest_result = $m_signup->findAll(array('si_id'=>0), 'signid DESC', '', 20);
			$this->invite_id = intval($_GET['invite_id']);/**/
			$m_weibo = spClass('m_weibo');
			
			$this->mom_result = $m_weibo->findAll(array('type'=>2));
			$this->self_result = $m_weibo->findAll(array('type'=>3));
			$this->man_result = $m_weibo->findAll(array('type'=>4));
			
			$this->display('zt/38.html');
		}

		function sign()
		{		
			$visitor_id = $_SESSION['visitor_id'];
			if(!$visitor_id) {
				exit('-1');		//没登陆跳出，前台跳转登陆
			}
			
			$m_member = spClass("m_member");
			$result = $m_member->find(array('visitor_id'=>$visitor_id));
		
			if(!$result) {
				return false;
			}
			$m_signup = spClass('m_signup');
			$invite_id = intval($_POST['invite_id']);
			
			/*
			 * 根据需要，这里是只判断当天是否参加活动
			$condition = "`visitor_id`='$visitor_id' AND DATE_FORMAT(`sign_date`, '%Y-%m-%d')=CURDATE()";			if($m_signup->find($condition)) {
				exit('-2');		//如果当天已经申请过了，将不会有报名资格
			}
			*/

			/*
			 *	根据需要，这里判断如果报名参加过就不能参加
			 */
			if($m_signup->find(array('visitor_id'=>$visitor_id, 'si_id'=>0))) {
				exit('-2');		//如果已经申请过了，将不会有报名资格
			}
			$visitor_nick = $_SESSION['visitor_nick'];

			$r = $m_signup->create(array('visitor_id'=>$visitor_id, 'visitor_nick'=>$visitor_nick,  'sign_date'=>date('Y-m-d H:i:s', time()), 'sign_ip'=>ip()));//插入到奖票表

			if($invite_id > 0) {
				if($visitor_id == $invite_id) {
					return false;		//当被邀请来后，判断被邀请者与邀请者为同一人不增加机会
				}
				
				$invite_result = $m_member->find(array('visitor_id'=>$invite_id));
				if(!$invite_result) {
					return false;
				}
				$m_sign_invite = spClass('m_sign_invite');
				$arr_sign_invite = array('invitor_id'=>$invite_id, 'invitee_id'=>$visitor_id, 'sign_ip'=>ip());		//插入邀请表
				$si_id = $m_sign_invite->create($arr_sign_invite);
				$m_signup->create(array('visitor_id'=>$invite_result['visitor_id'], 'visitor_nick'=>$invite_result['visitor_nick'], 'si_id'=>$si_id, 'sign_date'=>date('Y-m-d H:i:s', time()), 'sign_ip'=>ip()));//插入到奖票表
			}
			exit(str_pad($r, 5, '0', STR_PAD_LEFT));
		}

		function add_ecrm()
		{
			if(!empty($_POST)) {
				$m_ecrm = spClass('m_ecrm');
				$m_attachment = spClass('attachment');
				$_POST['thumb'] = $m_attachment->upload('thumb');
				if(!empty($_POST['pos'])) {
					$_POST['pos'] = implode(",", $_POST['pos']);
				}
				$m_ecrm->create($_POST);
				$this->success('提交成功', spUrl('signup', 'ecrmlist'));
			} else {
				$m_store = spClass('m_store');
				$store_id = intval($_GET['store_id']);
				$this->store_result = $m_store->find(array('store_id'=>$store_id));
				
				$this->display('admin/add_ecrm.html');
			}
		}

		function ecrm_edit()
		{
			$m_ecrm = spClass('m_ecrm');
			if(!empty($_POST)) {
				$activity_id = intval($_POST['activity_id']);
				if($_FILES['thumb']['size'] > 0) {
					$m_attachment = spClass('attachment');
					$_POST['thumb'] = $m_attachment->upload('thumb');
				}
				if(!empty($_POST['pos'])) {
					$_POST['pos'] = implode(",", $_POST['pos']);
				}
				$arr_erm = array('store_id'=>$_POST['store_id'], 'store_name'=>$_POST['store_name'], 'title'=>$_POST['title'], 'seller_id'=>intval($_POST['seller_id']), 'nums'=>intval($_POST['nums']));
				$m_ecrm->update(array('activity_id'=>$activity_id), $_POST);
				$this->success('提交成功', spUrl('signup', 'ecrmlist'));
			} else {
				$activity_id = intval($_GET['activity_id']);
				if($activity_id < 1) return false;
				$this->result = $m_ecrm->find(array('activity_id'=>$activity_id));
				if($this->result['pos']) {
					$this->arr_pos = explode(',', $this->result['pos']);
				}
				$this->display('admin/edit_ecrm.html');
			}
		}

		function ecrmlist()
		{
			$m_ecrm = spClass('m_ecrm');
			$this->results = $m_ecrm->spPager($this->spArgs('page', 1), 10)->findAll();
			$this->display('admin/ecrm_list.html');
		}

		function get_ecrm()
		{
			$visitor_id = $_SESSION['visitor_id'];
			$visitor_nick = $_SESSION['visitor_nick'];
			$activity_id = intval($_GET['activity_id']);
			/*
			if(!$visitor_id) {
				$url = 'https://login.taobao.com//member/login.jhtml?f=top&redirectURL=' .  urlencode(spUrl('signup', 'get_ecrm', array('activity_id'=>$activity_id)));
				$this->error('请登录后再来访问', spUrl('signup', 'get_ecrm'));
			}
			*/
			if($activity_id < 1) return false;

			$m_premium_user = spClass('m_premium_user');
			if($visitor_id != 0) {
				if($m_premium_user->find(array('activity_id '=>$activity_id, 'visitor_id'=>$visitor_id))) {
					//$this->error('你已领取优惠券', 'http://yingyong.taobao.com/show.htm?app_id=183002');
				}
			}
			

			$m_ecrm = spClass('m_ecrm');
			$result = $m_ecrm->find(array('activity_id'=>$activity_id));

			$url = 'http://ecrm.taobao.com/shopbonusapply/buyer_apply.htm?activity_id= ' .  $result['activity_id'] . '&seller_id=' . $result['seller_id'];
			
			$preminu_result = spAccess('r',  'ad_tmp_result');

			if($preminu_result) {
				$preminu_result['valid_date'] = '有效日期：' . datetoch($result['startdate']) . '-' . datetoch($result['enddate']);
				$preminu_result['use_count'] = $m_premium_user->findCount(array('activity_id'=>$result['activity_id']));
				spAccess('w',  'ad_tmp_result', $preminu_result, 86400);
			}

			if($visitor_id) {
				$m_premium_user->create(array('activity_id'=>$activity_id, 'visitor_id'=>$visitor_id, 'visitor_nick'=>$visitor_nick));
			} else {
				$m_premium_user->create(array('activity_id'=>$activity_id));
			}
			$this->jump($url);
		}

		function tofront()
		{
			$m_ecrm = spClass('m_ecrm');
			$activity_id = intval($_GET['activity_id']);
			if($activity_id < 1) return false;
			if($_GET['isfront'] == 1) {
					$m_ecrm->update(array('activity_id'=>$activity_id), array('isfront'=>0));
			} else {
				$m_ecrm->update(array('activity_id'=>$activity_id), array('isfront'=>1));
			}
			$result = $m_ecrm->find(array('isfront'=>1), 'activity_id DESC');
			if($result['activity_id']) {
				$result['premium_url'] = spUrl('signup', 'get_ecrm', array('activity_id'=>$result['activity_id'])); //转化活动id
				if($result['store_id']) {
					$m_taobao = spClass('m_taobao');
					$tbshopconvert = new TaobaokeShopsConvertRequest;
					$tbshopconvert->setFields("click_url,shop_type");
					$tbshopconvert->setSids($result['store_id']);
					$tbshopconvert->setNick("半价汇");
					$convert_resp = $m_taobao->m_topsdk->execute($tbshopconvert);
					$result['storelink'] = $convert_resp->taobaoke_shops->taobaoke_shop[0]->click_url;
				}
				spAccess('w',  'ad_tmp_result', $result, 86400);
			}
			$this->success('提交成功', spUrl('signup', 'ecrmlist'));
		}
	}
?>